/*******************************************************************************
 * Copyright (c) 2011 TXT e-solutions SpA
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * This work was performed within the IoT_at_Work Project
 * and partially funded by the European Commission's
 * 7th Framework Programme under the research area ICT-2009.1.3
 * Internet of Things and enterprise environments.
 *
 *
 * Authors:
 *     Cristoforo Seccia (TXT e-solutions SpA)
 *
 * Contributors:
 *      Domenico Rotondi (TXT e-solutions SpA)
 *******************************************************************************/
package it.txt.access.capability.commons.schema.validation;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.ValidationEvent;
import javax.xml.bind.ValidationEventHandler;

/**
 *
 * @author Cristoforo Seccia (TXT e-solutions SpA)
 */
public class CapabilitySchemaValidationHandler implements ValidationEventHandler {

    private static final Logger logger = Logger.getLogger(CapabilitySchemaValidationHandler.class.getName());

    private List<ValidationEvent> validationEvents = new ArrayList<ValidationEvent>();

    private static final String NEW_LINE = "\n";

    public boolean handleEvent(ValidationEvent event) {
        validationEvents.add(event);
        return true;
    }

    public List<ValidationEvent> getValidationEvents() {
        return validationEvents;
    }

    public void invalidateValidationEvents() {
        validationEvents.removeAll(validationEvents);
    }

    public void logValidationEvents() {
        if (validationEvents.size() > 0) {
            logger.severe("\n" + 
                    "***********************************************\n" +
                    "Validation errors occurred: \n" +
                    "***********************************************\n");
            for (ValidationEvent validationEvent : validationEvents) {
                String eventDump = dumpValidationEvent(validationEvent);
                logger.severe(eventDump);
            }
            logger.severe("***********************************************");
        }
        else {
            if (logger.isLoggable(Level.FINER))
                logger.finer("\n" +
                    "***********************************************\n" +
                    "No Validation Errors \n" +
                    "***********************************************\n");
        }
    }

    public static String dumpValidationEvents(List<ValidationEvent> validationEvents) {
        StringBuilder builder = new StringBuilder();
        if (validationEvents.size() > 0) {
            builder.append("\n" +
                    "***********************************************\n");
            for (ValidationEvent validationEvent : validationEvents) {
                String eventDump = dumpValidationEvent(validationEvent);
                builder.append(eventDump);
            }
            builder.append("***********************************************");
        }
        else {
            builder.append("\n" +
                    "***********************************************\n" +
                    "No Validation Errors \n" +
                    "***********************************************\n");
        }
        return builder.toString();
    }

    private static String dumpValidationEvent(ValidationEvent event) {
        StringBuilder builder = new StringBuilder();
        builder.append("EVENT");
        builder.append(NEW_LINE);
        builder.append("SEVERITY:  ");
        builder.append(event.getSeverity());
        builder.append(NEW_LINE);
        builder.append("MESSAGE:  ");
        builder.append(event.getMessage());
        builder.append(NEW_LINE);
        builder.append("LINKED EXCEPTION:  ");
        builder.append(event.getLinkedException());
        builder.append(NEW_LINE);
        builder.append("LOCATOR");
        builder.append(NEW_LINE);
        builder.append("    LINE NUMBER:  ");
        builder.append(event.getLocator().getLineNumber());
        builder.append(NEW_LINE);
        builder.append("    COLUMN NUMBER:  ");
        builder.append(event.getLocator().getColumnNumber());
        builder.append(NEW_LINE);
        builder.append("    OFFSET:  ");
        builder.append(event.getLocator().getOffset());
        builder.append(NEW_LINE);
        builder.append("    OBJECT:  ");
        builder.append(event.getLocator().getObject());
        builder.append(NEW_LINE);
        builder.append("    NODE:  ");
        builder.append(event.getLocator().getNode());
        builder.append(NEW_LINE);
        builder.append("    URL:  ");
        builder.append(event.getLocator().getURL());
        builder.append(NEW_LINE);
        return builder.toString();
    }
}
